/*
 * Copyright 2012 Quytelda Gaiwin
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.tamalin.panthersleek;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Locale;
import java.util.Scanner;
import java.util.logging.*;

/**
 * The Panther Sleek main class.
 * @author Quytelda K. Gaiwin
 */
public class Main
{
	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args)
	{
		// Parse command line arguments
		// All arguments have both a short and long form
		for(int i = 0; i < args.length; i++)
		{
			if(args[i].equals("-h") || args[i].equals("--help"))
			{
				printHelp();
			}
			if(args[i].equals("-c"))
			{
				// set encryption algorithm name to the next argument in the list
				calg = args[++i];
				Logger.getLogger(Main.class.getName()).log(Level.INFO, "Changing cipher algorithm to {0}", calg);
			}
			if(args[i].equals("-d"))
			{
				// set digest algorithm name to the next argument in the list
				dalg = args[++i];
				Logger.getLogger(Main.class.getName()).log(Level.INFO, "Changing cipher algorithm to {0}", dalg);
			}
			if(args[i].equals("-v"))
			{
				// enable verbose mode
				// create a new logger to represent the package org.tamalin.panthersleek
				// then add a new ConsoleHandler to allow messages as fine as Level.FINEST
				// then prevent the logger from using its parent handlers to create duplicate messages.
				Logger pkgLogger = Logger.getLogger(Main.class.getPackage().getName());
				pkgLogger.setLevel(Level.FINEST);
				ConsoleHandler handler = new ConsoleHandler();
				handler.setLevel(Level.FINEST);
				pkgLogger.addHandler(handler);
				pkgLogger.setUseParentHandlers(false);
				
				Logger.getLogger(Main.class.getName()).fine("Verbose mode enabled");
			}
			else if(args[i].equals("-q") || args[i].equals("--quiet"))
			{
				// enables quiet mode
				// create a new logger to represent the package org.tamalin.panthersleek
				// then add a new ConsoleHandler to allow messages as fine as Level.WARNING
				// then prevent the logger from using its parent handlers to create duplicate messages.
				Logger pkgLogger = Logger.getLogger(Main.class.getPackage().getName());
				pkgLogger.setLevel(Level.WARNING);
				ConsoleHandler handler = new ConsoleHandler();
				handler.setLevel(Level.WARNING);
				pkgLogger.addHandler(handler);
				pkgLogger.setUseParentHandlers(false);
				
				Logger.getLogger(Main.class.getName()).fine("Verbose mode enabled");
			}
			else if(args[i].equals("-V") || args[i].equals("--version"))
			{
				printVersion(true);
				System.exit(0);
			}
			else
			{
				System.err.println("Invalid argument: " + args[i] + "\nTry --help for details.");
				System.exit(0);
			}
		}

		// Configure for use on Mac OS X.
		// Move the menu bar to the top of the screen
		// and set the application name to "Panther Sleek"
		if (System.getProperty("os.name").contains("Mac"))
		{
			System.setProperty("apple.laf.useScreenMenuBar", "true");
			System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Panther Sleek");
		}
		
		Panther p = new Panther(calg, dalg);
		p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		p.setVisible(true);
	}

	/**
	 * Prints help/usage message to console.  This method is invoked when the help arguments
	 * are provided or when there is an invalid argument provided.
	 */
	public static void printHelp()
	{
		URL url = Main.class.getResource("/org/tamalin/panthersleek/resources/international/help_" + Locale.getDefault().getLanguage() + ".txt");
		try
		{
			URLConnection urlc = url.openConnection();
			Scanner scanner = new Scanner(urlc.getInputStream());
			while (scanner.hasNextLine())
			{
				System.out.println(scanner.nextLine());
			}
		}
		catch (IOException ex)
		{
			Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Unable to open help stream.", ex);
		}

	}

	/**
	 * Prints program version and licensing information.
	 * 
	 */
	public static void printVersion(boolean includeNotice)
	{
		System.out.println("Panther Sleek " + Panther.VERSION);
		System.out.println("Copyright (C) Quytelda Gaiwin\n");

		if (includeNotice)
		{
			System.out.println("This software is available, free of charge, under the terms of the\n"
					+ "Apache 2.0 License (http://www.apache.org/licenses/LICENSE-2.0).\n");
		}
	}
	
	/*******************
	* Runtime Settings *
	********************/
	private static String calg = "AES", dalg = "SHA-1";
}
